package com.aaa.inbound.service.impl;

import com.aaa.entity.inbound.ReceiveDetail;
import com.aaa.inbound.mapper.ReceiveDetailMapper;
import com.aaa.inbound.service.ReceiveDetailService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author aaa
 * @since 2020-11-25
 */
@Service
public class ReceiveDetailServiceImpl extends ServiceImpl<ReceiveDetailMapper, ReceiveDetail> implements ReceiveDetailService {

    @Autowired
    private ReceiveDetailMapper receiveDetailMapper;

    @Override
    public boolean saveDetail(ReceiveDetail detail) {
        if (detail.getId()!=null){
            if (findById(detail.getPid(),detail.getCommodityid())){
                receiveDetailMapper.updDetail(detail.getCommodityid(), detail.getQty());
                return true;
            }
        }
        receiveDetailMapper.insert(detail);
        return false;
    }

    public boolean findById(Long pid,Long commodityid) {
        QueryWrapper<ReceiveDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("pid",pid);
        wrapper.eq("commodityid",commodityid);
        ReceiveDetail receiveDetail = receiveDetailMapper.selectOne(wrapper);
        if (receiveDetail==null){
            return false;
        }else {
            return true;
        }
    }
}
